#!/usr/bin/env bash
PATH=.:$PATH:/usr/sbin
MAKE=${MAKE:-make}

export PDNS=${PDNS:-${PWD}/../pdns/pdns_server}
export PDNS2=${PDNS2:-${PWD}/../pdns/pdns_server}
export PDNSRECURSOR=${PDNSRECURSOR:-${PWD}/../pdns/recursordist/pdns_recursor}
export RECCONTROL=${RECCONTROL:-${PWD}/../pdns/recursordist/rec_control}
export SDIG=${SDIG:-${PWD}/../pdns/sdig}
export NOTIFY=${NOTIFY:-${PWD}/../pdns/pdns_notify}
export NSEC3DIG=${NSEC3DIG:-${PWD}/../pdns/nsec3dig}
export SAXFR=${SAXFR:-${PWD}/../pdns/saxfr}
export ZONE2SQL=${ZONE2SQL:-${PWD}/../pdns/zone2sql}
export ZONE2JSON=${ZONE2JSON:-${PWD}/../pdns/zone2json}
export ZONE2LDAP=${ZONE2LDAP:-${PWD}/../pdns/zone2ldap}
export PDNSUTIL=${PDNSUTIL:-${PWD}/../pdns/pdnsutil}
export PDNSCONTROL=${PDNSCONTROL:-${PWD}/../pdns/pdns_control}

unset _JAVA_OPTIONS

spectest=$1
[ -z $spectest ] && spectest=""

for prog in $SDIG $SAXFR $NOTIFY $NSEC3DIG; do
  if `echo $prog | grep -q '\.\./pdns'`; then
    ${MAKE} -C ../pdns ${prog##*../pdns/} || exit
  fi
done

export SDIG="timeout 5 $SDIG"
export NSEC3DIG="timeout 5 $NSEC3DIG"
export SAXFR="timeout 30 $SAXFR"
export PDNSCONTROL="timeout 5 $PDNSCONTROL"

rm -f test-results failed_tests passed_tests skipped_tests ${testsdir}/*/real_result ${testsdir}/*/diff ${testsdir}/*/*.out ${testsdir}/*/start ${testsdir}/*/step.*

passed=0
failed=0
skipped=0

touch passed_tests failed_tests skipped_tests

for a in $(find $testsdir -type d | grep -v ^.$ | grep -v .svn | grep -v ^confdir | LC_ALL=C sort) 
do
	if [ ! -x $a/command ]
	then
		continue
	fi
	testname=$(basename $a)
	export testname
	echo  "$testname: "
	cat $a/description 

	echo  "$testname: " >> test-results
	cat $a/description >> test-results
	
	SKIPIT=0
	if [ -e $a/skip ] 
	then
		SKIPIT=1
		result="	Skipped test $a"
	else
		for reason in $skipreasons $context
		do
			if [ -e $a/skip.$reason ]
			then
				SKIPIT=1
				result="	Skipped test $a for reason $reason"
				break
			fi
		done
	fi

	FAIL=0
	for reason in $skipreasons $context
	do
		if [ -e $a/fail.$reason ]
		then
			FAIL=1
			break
		fi
	done

	if [ "$spectest" != "" ] && [ "$spectest" != "$testname" ] && [ "$testname" != "00dnssec-grabkeys" ]
	then
		SKIPIT=1
		result="	Skipped test $a because it's not the specified single test"
	fi

				
	if [ $SKIPIT = 1 ]
	then
		echo $testname >> skipped_tests
		skipped=$[$skipped+1]
	else
		$a/command > $a/real_result
		expected=$a/expected_result

		diffopts="-u"
		if [ -e $a/expected_result.i ]; then
			expected=$a/expected_result.i
			diffopts="${diffopts} -i"
		fi

		for extracontext in $extracontexts
		do
			[ -e "$a/expected_result.$extracontext" ] && expected=$a/expected_result.$extracontext
		done
		[ -n "$context" ] && [ -e "$a/expected_result.$context" ] && expected=$a/expected_result.$context
		diff ${diffopts} $expected $a/real_result > $a/diff 2>&1
		if [ -s $a/diff ] 
		then
			if [ $FAIL = 0 ]
			then
				result="	Failed test $a"
				echo $testname >> failed_tests
				failed=$[$failed+1]
			else
				result="	Expected failure for reason $reason, test passed $a"
				echo $testname >> passed_tests
				passed=$[$passed+1]
			fi
		else
			if [ $FAIL = 0 ]
			then
				result="	Passed test $a"
				echo $testname >> passed_tests
				passed=$[$passed+1]
			else
				result="	Unexpected pass for reason $reason, test failed $a"
				echo $testname >> failed_tests
				failed=$[$failed+1]
			fi
		fi
	fi	
	echo "$result"
	echo
	echo "$result" >> test-results
	echo >> test-results
done

echo -n $passed out of $[$passed+$failed]
echo -n " ("
res=$((echo scale=2; echo 100*$passed/\($passed+$failed\)) | bc )
echo -n "$res%) "
echo tests passed, $skipped were skipped
